﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Web.UI;
using System.Web.UI.WebControls;
using Customer_Profile.eCard.DBDao;
using KVB.Common;
using DevExpress.Web.ASPxGridView;

namespace Customer_Profile.eCard
{
    public partial class CpartyBank : System.Web.UI.Page
    {
        string strCpartyID = "";
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Request["cparty_id"] != "")
                strCpartyID = Request["cparty_id"].ToString();
            if (!Page.IsPostBack)
            {
                //gridBank
                bindGridBank(strCpartyID);                
            }
        }

        private void bindGridBank(string CpartyID)
        {
            try
            {
                DataSet ds = CpartyDao.getCpartyBank(CpartyID);
                if (ds != null && ds.Tables.Count > 0)
                {
                    gridBank.DataSource = ds.Tables[0];
                    gridBank.DataBind();
                }
                else
                {
                    gridBank.DataBind();
                }
            }
            catch (Exception ex)
            {
                Log.WriteDefaultLog("bindGrid", ex.ToString());
            }
        }

        private bool checkform()
        {
            if (accountname.Text.Trim().Equals(""))
            {
                lblWarning.Text = "账户名称不能为空";
                return false;
            }
            if (accountno.Text.Trim().Equals(""))
            {
                lblWarning.Text = "账户号不能为空";
                return false;
            }
            if (bankname.Text.Trim().Equals(""))
            {
                lblWarning.Text = "银行名称不能为空";
                return false;
            }
            if (branch.Text.Trim().Equals(""))
            {
                lblWarning.Text = "支行名称不能为空";
                return false;
            }
            if (subbranch.Text.Trim().Equals(""))
            {
                lblWarning.Text = "分行名称不能为空";
                return false;
            }
            if (location.Text.Trim().Equals(""))
            {
                lblWarning.Text = "地区不能为空";
                return false;
            }
            if (city.Text.Trim().Equals(""))
            {
                lblWarning.Text = "城市不能为空";
                return false;
            }
            return true;
        }
        protected void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                if (checkform())
                {
                    CpartyDao.addCpartyBank(strCpartyID, accountname.Text, accountno.Text, bankname.Text, branch.Text, subbranch.Text, location.Text, city.Text);
                    bindGridBank(strCpartyID);
                    lblWarning.Text = "添加银行信息成功";
                }
            }
            catch (Exception ex)
            {
                Log.WriteDefaultLog("CpartyBank.btnSave_Click", ex.ToString());
                lblWarning.Text = "添加银行信息失败！"+ex.ToString();
            }
        }

        protected void btnUpate_Click(object sender, EventArgs e)
        {
            try
            {
                if (checkform())
                {
                    string cpartyAccountId = hidid.Value;
                    //Log.WriteDefaultLog("cpartyAccountId:", cpartyAccountId);
                    CpartyDao.updateCpartyBank(cpartyAccountId, accountname.Text, accountno.Text, bankname.Text, branch.Text, subbranch.Text, location.Text, city.Text, ASPxComboBox1.SelectedItem.Value.ToString());
                    bindGridBank(strCpartyID);
                    lblWarning.Text = "更新银行信息成功";
                }
            }
            catch (Exception ex)
            {
                Log.WriteDefaultLog("CpartyBank.btnUpate_Click", ex.ToString());
                lblWarning.Text = "更新银行信息失败"+ex.ToString();
            }
        }
    }
}